import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watcher.server import WatcherServer

if __name__ == "__main__":
    from optparse import OptionParser
    usage = "usage: %prog {-n, --name} servername {-p, --path} configpath"
    parser = OptionParser(usage)
    parser.add_option("-n", "--name", dest="name", help="name of the server tostart")
    parser.add_option("-p", "--path", dest="path", help="configuration path")
    (options, args) = parser.parse_args()
    name = options.name
    configPath = options.path
    if not name:
        parser.error("Must have server name")


    from twisted.web import server, resource
    """run server"""
    root = resource.Resource()
    ws = WatcherServer(name, configPath)
    root.putChild('RPC2', ws)
    site = server.Site(root)
    ws.reactor.listenTCP(int(ws.config.listen_port.xml_text_content()), site)
    ws.reactor.run()
